package ttc.mBackup.BackupAndRestoreSMS;

import java.util.ArrayList;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.Toast;

public class SMSListArrayAdapter extends ArrayAdapter<SMSClassCustomViewGroup>
{
	ArrayList<String> arrayString;
	ArrayList<SMSClassCustomViewGroup> array;
	int resource;
	Context context;
	
	public SMSListArrayAdapter(Context context,int textViewResourceId,ArrayList<SMSClassCustomViewGroup>objects)
	{
		super(context,textViewResourceId,objects);
		this.context=context;
		resource=textViewResourceId;
		array=objects;
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		View smsView = convertView;
		if(smsView==null)
		{
			smsView=new SMSCustomViewGroup(getContext());
		}
		final SMSClassCustomViewGroup smsClassCustomView=array.get(position);
		if(smsClassCustomView!=null)
		{
			TextView body= ((SMSCustomViewGroup)smsView).txtSMSBody;
			TextView address= ((SMSCustomViewGroup)smsView).txtPhoneContact;
			TextView id =((SMSCustomViewGroup)smsView).txtIdSMS;
			CheckBox checkSMS=((SMSCustomViewGroup)smsView).cbSMS;
		
			checkSMS.setOnCheckedChangeListener(new OnCheckedChangeListener() {
				
				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					// TODO Auto-generated method stub
					smsClassCustomView.setChecked(isChecked);
				}
			});
			body.setText(smsClassCustomView.getBody());
			address.setText(smsClassCustomView.getAddress());
			id.setText(smsClassCustomView.getId());
			checkSMS.setChecked(smsClassCustomView.isChecked());
			
			smsView.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					if(((SMSCustomViewGroup)v).cbSMS.isChecked())
					{
						((SMSCustomViewGroup)v).cbSMS.setChecked(false);
						Toast.makeText(context,	(CharSequence)(((SMSCustomViewGroup)v).txtIdSMS.toString()), Toast.LENGTH_LONG);
					}
					else
					{
						((SMSCustomViewGroup)v).cbSMS.setChecked(true);
						Toast.makeText(context,	(CharSequence)(((SMSCustomViewGroup)v).txtPhoneContact.getText()) , Toast.LENGTH_LONG);
						Log.i("PhoneContact", ((SMSCustomViewGroup)v).txtPhoneContact.getText().toString());
						Log.i("IdSMS", ((SMSCustomViewGroup)v).txtIdSMS.getText().toString());
					}
					smsClassCustomView.setChecked(true);
					
				}
			});
		}
		return smsView;
	}
	public ArrayList<String> getIdSMSSelected(String strIdSMS)
	{
		arrayString.add(strIdSMS);
		return arrayString;
	}
}
